
//
//  UIImage+PPKit.swift
//  PPKit
//
//  Created by xiaopin on 2019/8/2.
//  Copyright © 2019 PPKit. All rights reserved.
//  https://gitee.com/xiaopindev/PPKit
//  https://github.com/xiaopindev
//  https://xiaopin.cnblogs.com

import UIKit

/*
 知识点：class 和 static 都可以创建类方法，区别是：class可以被子类继承重写
 */
extension UIImage{
    /// 通过颜色快捷生成图片
    /// - Parameters:
    ///   - color: 颜色
    ///   - size: 尺寸大小
    convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
        let rect = CGRect(origin: .zero, size: size)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        color.setFill()
        UIRectFill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        guard let cgImage = image?.cgImage else { return nil }
        self.init(cgImage: cgImage)
    }
    
//    class func named(named:String?) -> UIImage? {
//        let image = UIImage(named:named ?? "")
//        if image == nil {
//            debugPrint("❌找不到\(named ?? "").png文件")
//        }
//        return image
//    }
    
    //MARK: 纯色转图片
    
    /// 纯色转图片
    /// - Parameter color: <#color description#>
    /// - Returns: <#description#>
    static func colorImage(_ color:UIColor) -> UIImage?{
        let size = CGSize(width: 1, height: 1)
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(color.cgColor)
        context?.fill(CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }

    /// 创建纯色转图片
    ///
    /// - Parameters:
    ///   - color: <#color description#>
    ///   - size: <#size description#>
    /// - Returns: <#return value description#>
    static func colorImage(_ size:CGSize,_ color:UIColor) -> UIImage?{
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(color.cgColor)
        context?.fill(CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    
    /// 创建纯色转图片
    ///
    /// - Parameters:
    ///   - size: <#size description#>
    ///   - color: <#color description#>
    /// - Returns: <#return value description#>
    static func hex16Image(_ size:CGSize, _ color:String) -> UIImage?{
        return UIImage.colorImage(size, UIColor.hexColor(color))
    }
    
    //MARK: 渐变色转图片
    /// 渐变图片生成
    ///
    /// - Parameters:
    ///   - size: <#size description#>
    ///   - startColor: <#startColor description#>
    ///   - endColor: <#endColor description#>
    ///   - isHorizontal: <#isHorizontal description#>
    /// - Returns: <#return value description#>
    static func gradientImage(_ size:CGSize,_ startColor:UIColor,_ endColor:UIColor,_ isHorizontal:Bool = true) -> UIImage?{
        UIGraphicsBeginImageContextWithOptions(size, true, 1)
        //获取绘图上下文
        guard let context = UIGraphicsGetCurrentContext() else {
            return nil
        }
        context.saveGState()
        
        //使用rgb颜色空间
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        
        //颜色数组
        let startComponents:[CGFloat] = startColor.cgColor.components!
        let endComponents:[CGFloat] = endColor.cgColor.components!
        let components:[CGFloat] = startComponents + endComponents
        
        //没组颜色所在位置（范围0~1)
        let locations:[CGFloat] = [0,1]
        //生成渐变色（count参数表示渐变个数）
        let gradient = CGGradient(colorSpace: colorSpace, colorComponents: components, locations: locations, count: locations.count)
        
        //渐变开始位置
        let start = CGPoint(x: 0, y: 0)
        //渐变结束位置
        let end = isHorizontal ? CGPoint(x: size.width, y: 0) : CGPoint(x: 0, y: size.height)
        //绘制渐变
        context.drawLinearGradient(gradient!, start: start, end: end, options: CGGradientDrawingOptions.drawsBeforeStartLocation.union(.drawsAfterEndLocation))
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
    
        context.restoreGState()
        UIGraphicsEndImageContext()
        
        return image
    }
    
    /// 渐变图片
    ///
    /// - Parameters:
    ///   - size: <#size description#>
    ///   - startColor: 16进制颜色，比如：7E0064
    ///   - endColor: 16进制颜色，比如：7E0064
    ///   - isHorizontal: <#isHorizontal description#>
    /// - Returns: <#return value description#>
    static func gradientImage(_ size:CGSize,_ startColor:String,_ endColor:String,_ isHorizontal:Bool = true) -> UIImage?{
        return UIImage.gradientImage(size, UIColor.hexColor(startColor), UIColor.hexColor(endColor), isHorizontal)
    }
}
